package com.liping.www.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liping.www.config.LiPingInfo;
import com.liping.www.entity.Illness;
import com.liping.www.entity.Medicine;
import com.liping.www.entity.User;
import com.liping.www.service.IllnessKindService;
import com.liping.www.service.IllnessService;
import com.liping.www.service.MedicineService;
import com.liping.www.util.Assert;
import com.liping.www.vo.request.FeedBackRequestVo;
import com.liping.www.vo.response.MyHahMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.List;

/*
* @Controller
* 表示该类是一个控制器 MVC中的C层
* 可以处理前端浏览器的请求
* */
@Controller
public class IndexController {

    //自动注入配置类
    @Autowired
    private LiPingInfo liPingInfo;
    @Autowired
    private IllnessService illnessService;
    @Autowired
    private IllnessKindService illnessKindService;
    @Autowired
    private MedicineService medicineService;

    //表示GET请求/index的URL会到达该方法
    @RequestMapping("/index")
    //参数非必填，内部实际转为null，所以要求类型是引用类型【自定义数据类型、包装类】
    //不可以是基本的数据类型
    //类型
    //如何设置参数参数的必填【业务层面数据合法性】
    //参数名称与前端完全一致？？a---->abc
    //@RequestParam(name = "d",required = true) String ie 表示1前端参数d映射为变量ie，同时要求必填
    //Vo方法
    public String index(Model model, FeedBackRequestVo feedBackRequestVo){
        System.out.println(feedBackRequestVo);

        System.out.println("配置信息："+liPingInfo);
        model.addAttribute("echo",liPingInfo);
        //业务逻辑代码
        //返回结果【数据】

        System.out.println("表示GET请求/index的URL会到达该方法");
        //设置数据回显到页面
        //第一种简单的数据 参数名=参数值
        model.addAttribute("username","23311147李萍");
        //第二种 自定义的数据类型
        FeedBackRequestVo feedBackRequestVo1 = new FeedBackRequestVo();
        feedBackRequestVo1.setName("李萍");
        feedBackRequestVo1.setEmail("123@qq.com");
        feedBackRequestVo1.setTitle("留言标题");
        feedBackRequestVo1.setTitle("留言详细内容");
        model.addAttribute("feedBackInfo",feedBackRequestVo1);
        //第三种
//        HashMap<String,String>hashMap = new HashMap<>();
//        hashMap.put("name","测试Map");
//        hashMap.put("email","123@qq.com");
//        hashMap.put("title","留言标题");
//        hashMap.put("content","留言详细内容");

        MyHahMap myHahMap = new MyHahMap();
        myHahMap.put("测试Map","123@qq.com","留言详细内容","留言详细内容");
        model.addAttribute("mapInfo",myHahMap);
        return "index";
    }

//    @RequestMapping("/findIllness")
//    public String findIllness101(){
//        System.out.println("表示GET请求/index的URL会达到该方法");
//
//        return "index";//templates目录下文件名
//    }
//    @GetMapping("/profile")
//    public String profile(HttpSession session){
//        User loginUser = (User) session.getAttribute("loginUser");
//        if (Assert.isEmpty(loginUser)){
//            return "redirect:/index";
//        }
//        return "profile";
//    }

    @RequestMapping("/findIllness")
    public String findIllness101(Model model,String illnessName, @RequestParam(defaultValue = "1") Integer page){
        //读取illness表的数据 分页[每页显示的行数默认10，当前是第几页]
         IPage<Illness> dataByIllnessName = illnessService.getIllnessPageDataByIllnessName(illnessName,10,page);
         model.addAttribute("illness",dataByIllnessName.getRecords());
        System.out.println("表示GET请求/index的URL会到达该方法");
        return "search-illness";//templates目录下文件名
    }

    @RequestMapping("/findMedicines")
    public String findMedicines(Model model,String nameValue,@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        List<Medicine> medicineList= (List<Medicine>) medicineService.findMedicineByName(nameValue,currentPage,pageSize);
        model.addAttribute("medicineList",medicineList);
        System.out.println("表示GET请求/index的URL会到达");
        return "illness";
    }

//    @RequestMapping("/findMedicines")
//    public String findMedicines(Model model,String nameValue,@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
//        List<Medicine> medicineList= (List<Medicine>) medicineService.findMedicineByName(nameValue,currentPage,pageSize);
//        model.addAttribute("medicineList",medicineList);
//        System.out.println("表示GET请求/index的URL会到达");
//        return "illness";
//    }

    @RequestMapping("/all-illness")
    public String allIllness() {
        return "all-illness";
    }

    //药品管理
    @RequestMapping("/all-medical")
    public String allMedical(Model model,String medicineName, @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize) {
        List<Medicine> medicinePageDataByIllnessName = medicineService.findMedicineByName(medicineName,page,pageSize);
        model.addAttribute("medicines", medicinePageDataByIllnessName);
        return "all-medical";
    }

    @RequestMapping("/doctor")
    public String doctor(HttpSession session ){
        User loginUser = (User) session.getAttribute("loginUser");
        if (Assert.isEmpty(loginUser)){
            return "redirect:/index";
        }

        return "doctor";//templates目录下文件名
    }
    @RequestMapping("/feedback.html")
    public String feedback(){
        return "feedback";
    }

    //疾病管理
//    @RequestMapping("/all-illness")
//    public String illnessKind(){
//        return "all-illness";
//    }

//    @ModelAttribute
//    public void commonRes(Model model){
//        model.addAttribute("name","李萍");
//    }



    /**
     *
     * 退出登录
     *
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        //删除session
        session.invalidate();
        //页面跳转到/index
        return "redirect:/index";
    }
    @GetMapping("/profile")
    public String profile(HttpSession session){
        //先判断是否处于登录状态
        User loginUser = (User) session.getAttribute("loginUser");
        if (Assert.isEmpty(loginUser)){
            return "redirect:/index";
        }
        return "profile";
    }

}
